﻿package javase.thread;


//Demonstrate thread priorities.

class clicker implements Runnable {  //1
	int click = 0;
	Thread t;                 //2
	private volatile boolean running = true;

	public clicker(int p) {
		t = new Thread(this);   //3
		t.setPriority(p);
	}
	public void run() {
		while (running) {
			click++;
			
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}		
		}
	}
	public void stop() {
		running = false;
	}
	public void start() {
		t.start();   //4
	}
}

public class PriorityDemo {
	public static void main(String args[]) {
		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		clicker hi = new clicker(Thread.NORM_PRIORITY + 3);
		clicker lo = new clicker(Thread.NORM_PRIORITY - 3);
		System.out.println("Start ...");
		lo.start();
		hi.start();
		try {
			Thread.sleep(15000);
		} catch (InterruptedException e) {
			System.out.println("Main thread interrupted.");
		}
		lo.stop();
		hi.stop();
}
}